package com.aote.entity.component;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;

import com.aote.util.ThreadLocals;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.Tools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormComponent;
import com.browsesoft.htmlcomponent.HTMLPage;
import com.browsesoft.htmlcomponent.ReplacableParameterComponent;
import com.browsesoft.oa.DisposeSelectResult;

public class EntitySelectButton extends ReplacableParameterComponent implements
		DisposeSelectResult {
	// 判断是否第一次
	// private boolean first = true;

	// 选择的结果集合
	private List selectedEntity = new LinkedList();

	// 选择实体时要依据的属性名
	String attrName = new String();

	// 选择实体时要依据的属性值
	String attrValue = new String();

	// 选择实体的实体类型
	String entityType = new String();

	public EntitySelectButton(HTMLBasicComponent parent) throws Exception {
		super(parent);
	}

	/**
	 * 处理用户请求，将需要Model处理的内容提交给Model
	 * 
	 * @param request
	 *            客户端请求
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		this.selectedEntity.addAll(this.getAllEntity());
	}

	/**
	 * 找所有处理的实体
	 */
	private Collection getAllEntity() {
		// 找到所有需要处理的实体
		Element element = this.getElement();
		this.entityType = element.getAttribute("entitytype");
		this.attrName = element.getAttribute("attributename");
		HTMLBasicComponent parent = this.getParent();
		if (!(parent instanceof HTMLFormComponent)) {
			return null;
		}
		Entity entity = (Entity) parent.getModel();
		// 取要插入的属性值
		this.attrValue = element.getAttribute("attrvalue");
		if (this.attrValue == null || this.attrValue.equals("")) {
			this.attrValue = entity.getID();
		} 
		HTMLPage page = (HTMLPage) ThreadLocals.page.get();
		Collection collection = EntityManager.getInstance().getEntities(
				entityType, this.attrName, this.attrValue, page);
		return collection;
	}

	/**
	 * 处理实体 将要选择的实体全部找到，并把它的
	 */
	private void initEntity() {
		try {
			Iterator iter = this.getAllEntity().iterator();
			// 逐个将设置实体的属性
			while (iter.hasNext()) {
				Entity ent = (Entity) iter.next();
				ent.getAttributes().put(attrName, "0");
				ent.update();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 处理选择结果
	 */
	private void disposeSelected(List list) {
		try {
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Entity entity = (Entity) iter.next();
				entity.getAttributes().put(this.attrName, this.attrValue);
				entity.update();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void dispose(String selectId) throws Exception {
		if (this.selectedEntity.size() != 0) {
			this.selectedEntity.clear();
		}
		this.initEntity();
		this.disposeSelected(EntityManager.getEntities(selectId));
	}

	public void dispose(String name, String selectId) {
		// TODO Auto-generated method stub

	}

	public void dispose(Hashtable ht) {
		// TODO Auto-generated method stub

	}

	public String getDefaultValue() {
		Iterator iter = this.selectedEntity.iterator();
		String ids = Tools.entitiesToIDString(iter);
		return ids;
	}
}
